package com.example.studentinfomanager;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

public class StudentAdapter extends ArrayAdapter<Student> {
    private int resourceId;

    public StudentAdapter(Context context, int resourceId, List<Student> dataList) {
        super(context, resourceId, dataList);
        this.resourceId = resourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Student student = getItem(position);
        View view;
        ViewHolder holder;

        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
            holder = new ViewHolder();
            holder.tvName = view.findViewById(R.id.tvName);
            holder.tvId = view.findViewById(R.id.tvId);
            holder.tvGender = view.findViewById(R.id.tvGender);
            holder.tvScore = view.findViewById(R.id.tvScore);
            view.setTag(holder);
        } else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }

        holder.tvName.setText(student.getName());
        holder.tvId.setText(student.getStuid());
        holder.tvGender.setText(student.getGender());
        holder.tvScore.setText(student.getScore());

        return view;
    }

    static class ViewHolder {
        TextView tvName;
        TextView tvId;
        TextView tvGender;
        TextView tvScore;
    }
}